package com.ciwong.epaper.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.Subject;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.util.BaseExtCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/21.
 */

public class ItemSubjectType extends LinearLayout{

    private RecyclerView subject_recycler_view;


    private  HorizontalAdapter mHorizontalAdapter;

    private  OnSubjectSelectListener mOnSubjectSelectListener;
    private  Subject mSubject;

    public ItemSubjectType(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ItemSubjectType(Context context) {
        super(context);
        init();

    }


    private void init() {
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_subject_type, this);
        subject_recycler_view = (RecyclerView) findViewById(R.id.subject_recycler_view);
        subject_recycler_view.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mHorizontalAdapter = new HorizontalAdapter();
        subject_recycler_view.setAdapter(mHorizontalAdapter);


        mHorizontalAdapter.setRcyOnItemClickListener(new OnSubjectSelectListener() {
            @Override
            public void onSubjectSelect(int position,Subject subject) {

                mHorizontalAdapter.setSelection(position);
                mHorizontalAdapter.notifyDataSetChanged();

                if (null != mOnSubjectSelectListener) {
                    mOnSubjectSelectListener.onSubjectSelect(position,subject);
                }

            }
        });

    }

    public void setOnSubjectSelectListener(OnSubjectSelectListener listener) {
        mOnSubjectSelectListener=listener;

    }


    public void setSubjectList(List<Subject> list) {
        mHorizontalAdapter.setSubjectList(list);
    }

    /**
     * 内层的RecyclerView
     */
    private class HorizontalAdapter extends RecyclerView.Adapter<SubjectFilterHolder> {

        private List<Subject> subjectList = new ArrayList<>();
        private int clickSelectPos = 0;
        private  OnSubjectSelectListener rcyOnItemClickListener;

        private HorizontalAdapter(){
        }

        public void setSubjectList(List<Subject> list){
            subjectList=list;
            notifyDataSetChanged();
        }

        public void setRcyOnItemClickListener(OnSubjectSelectListener listener) {
            this.rcyOnItemClickListener = listener;
        }

        public void setSelection(int position){
            clickSelectPos = position;
        }

        @Override
        public SubjectFilterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = null;
            itemView = inflate(parent, R.layout.item_subject_filter);
            return  new SubjectFilterHolder(itemView);
        }

        @Override
        public void onBindViewHolder(SubjectFilterHolder holder, int position) {
            holder.mPosition=position;
            holder.mRcyOnItemClickListener=rcyOnItemClickListener;

            if (position < subjectList.size()) {
                holder.mSubject = subjectList.get(position);
                holder.tx_sub_kind_name.setText( subjectList.get(position).getSubjectName());
            }

            if (position == clickSelectPos) {
                holder.tx_sub_kind_name.setTextColor(getResources().getColor(R.color.color_light_green));
                holder.view_underline.setVisibility(View.VISIBLE);
            }else {
                holder.tx_sub_kind_name.setTextColor(getResources().getColor(R.color.light_gray17));
                holder.view_underline.setVisibility(View.GONE);
            }
        }

        @Override
        public int getItemCount() {
            if (null != subjectList) {
                return subjectList.size();
            }else{
                return 0;
            }
        }
    }



    static class SubjectFilterHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView tx_sub_kind_name;
        View view_underline;
        int mPosition=-1;
        Subject mSubject;

        OnSubjectSelectListener mRcyOnItemClickListener;
        public SubjectFilterHolder(View itemView) {
            super(itemView);
            tx_sub_kind_name = (TextView) itemView.findViewById(R.id.tx_sub_name);
            view_underline =  itemView.findViewById(R.id.view_underline);
            itemView.findViewById(R.id.handle_kind_tag).setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            if (null != mRcyOnItemClickListener && mPosition >= 0) {
                mRcyOnItemClickListener.onSubjectSelect(mPosition,mSubject);
            }
        }
    }

    public interface  OnSubjectSelectListener{
        void onSubjectSelect(int position, Subject subject);
    }

    protected View inflate(ViewGroup parent, int layoutRes) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
    }

    public void setGradeId(int gradeId,final  Subject subjectPara,int salesType,String area,int schoolId,int applyType) {
        EpaperDao.getInstance().getSubjectListByGrade(EApplication.BRAND_ID, gradeId,salesType,area,schoolId,applyType,
                new BaseExtCallBack(getContext()) {

                    @Override
                    public void failed(int errorCode, Object data) {
                        super.failed(errorCode, data);
//                        hideMiddleProgressBar();
                    }

                    @Override
                    public void failed(Object data) {
                        super.failed(data);
//                        hideMiddleProgressBar();
                    }

                    @Override
                    public void success(Object data) {
                        List<Subject> list=( List<Subject>)data;
                        if (null != list) {
                            Subject subject=new Subject();
                            subject.setSubjectName("range_all");
                            subject.setSubjectId(0);
                            list.add(0,subject);

                            mHorizontalAdapter.setSubjectList(list);
                            if(null !=subjectPara)
                            {
                                for(int i=0;i<list.size();i++)
                                {
                                    if(list.get(i).getSubjectId()==subjectPara.getSubjectId())
                                    {
                                        mHorizontalAdapter.setSelection(i);
                                    }
                                }

                            }else {
                                mHorizontalAdapter.setSelection(0);
                            }

                        }else {

                        }

                    }
                });
    }

}
